﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PhotoBox.PhotoBox_Webpart
{
    /// <summary>
    /// Class to encapsulate client side logic
    /// </summary>
    public class scriptClass
    {

        private string value = null;

        /// <summary>
        /// Culturize the title column to javascript UTF8 and setup the complaint http-equiv="X-UA-Compatible" to HTML5
        /// </summary>
        /// <param name="titleColumnCulture"></param>
        public scriptClass(string title)
        {
            value = "<script type=\"text/javascript\">" + "\n" +

            //"var m=document.getElementsByTagName('meta');" + "\n" +
            //"for(var c=0;c<m.length;c++) {" + "\n" +
            //"if(m[c].content=='IE=8') {" + "\n" +
            //"   m[c].content='IE=9'; " + "\n" +
            //"  } }" + "\n" +


            "$.support.cors = true;" + "\n" +
            "$.ajax({" + "\n" +
            "url: \"$url\"," + "\n" +
            "type: 'GET'," + "\n" +
            "dataType: 'JSON'," + "\n" +
            "crossDomain: true," + "\n" +
            "cache: true," + "\n" +
            "success: function (data) {" + "\n" +
                "var gallery = $('#gallery'), url;" + "\n" +
                "$.each(data.d.results, function (index, elem) {" + "\n" +
                //... do some work where ...
                    "url = elem.__metadata.media_src;" + "\n" +
                    "var img = $('<img>').prop({ 'src': url, 'title': elem." + title + " });" + "\n" +
                    "img.addClass(\"photobox\");" + "\n" +
                    "var link = document.createElement('a');" + "\n" +
                    "var li = document.createElement('li');" + "\n" +
                    "li.className += \"photobox\";" + "\n" +
                    "link.className += \"photobox\";" + "\n" +
                    "link.href = elem.OpenTo;" + "\n" +
                    "link.appendChild(img[0]);" + "\n" +
                    "li.appendChild(link);" + "\n" +
                    "gallery[0].appendChild(li);" + "\n" +

                    // lazy show the photos one by one
                    "img.on('load', function (e) {" + "\n" +
                     "   setTimeout(function () {" + "\n" +
                     "       li.className = 'loaded';" + "\n" +
                     "   }, 20 * index);" + "\n" +
                    "});" + "\n" +
                "});" + "\n" +
            "}," + "\n" +
            "error: function () {" + "\n" +
            "    alert(\"Error\");" + "\n" +
            "}" + "\n" +
        "});" + "\n" +
        "</script>";

        }

        /// <summary>
        /// Get the JSON script client to load async images from the sharepoint library
        /// </summary>
        /// <param name="listUrl"></param>
        /// <returns></returns>
        public string GetScript(string listUrl)
        {
            return this.value.Replace("$url", listUrl); 
        }

    }
}
